#! /bin/bash
export PYTHONIOENCODING=utf-8
export DEPLOY=stg
pwd
cd /root/carbon_metaverse_backend || exit 0
source venv/bin/activate # 使用虚拟环境
cd app || exit 0
if [ -z "$1" ]
then
    echo "Usages: sh server.sh [start|stop|restart]"
    exit 0
fi

if [ "$1" = start ]
then
    # shellcheck disable=SC2009
    # shellcheck disable=SC2126
    psid=$( ps aux | grep "carbon_meta_uwsgi.ini" | grep -v " grep" | wc -l )
    if [ "$psid" -gt 4 ]
    then
        echo "carbon_meta_uwsgi is running!"
        exit 0
    else
        nohup uwsgi ./carbon_meta_uwsgi.ini &
        echo "Start carbon_meta_uwsgi service [OK]"
    fi

elif [ "$1" = stop ];then
    uwsgi --stop pid
    echo "Stop carbon_meta_uwsgi service [OK]"
elif [ "$1" = restart ];then
    uwsgi --reload pid
    echo "Restart carbon_meta_uwsgi service [OK]"
elif [ "$1" = update ];then
    git pull
    echo "Update uwsgi service [OK]"
    uwsgi --reload pid
    echo "Restart carbon_meta_uwsgi service [OK]"
else
    echo "Usages: sh server.sh [start|stop|restart|update]"
fi
